package com.foreknow.elm.controller;

import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 啊滨
 * @date 2023/2/15 10:47
 */
public class CartController {

    public Object listCart(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        CartService service = new CartServiceImpl();
        return service.listCart(userId, businessId);
    }

    public Object saveCart(HttpServletRequest request) {
        int isRight = 0;
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        int foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        isRight = service.saveCart(userId, businessId, foodId);
        return isRight;
    }

    public Object updateCart(HttpServletRequest request) {
        int isRight = 0;
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        int foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        isRight = service.updateCart(userId, businessId, foodId,quantity);
        return isRight;
    }


    public Object removeCart(HttpServletRequest request){
        int isRight = 0;
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        int foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        isRight = service.removeCart(userId, businessId, foodId);
        return isRight;
    }
}
